//
//  HLMessageViewController.m
//  zhuanxian
//
//  Created by zhaoliang chen on 2017/11/16.
//  Copyright © 2017年 huiling. All rights reserved.
//

#import "HLMessageViewController.h"
#import "UITableView+Function.h"
#import "HLMessageCell.h"

@interface HLMessageViewController ()
<UITableViewDelegate, UITableViewDataSource>
{
    NSInteger pageNumber;
}

@property(nonatomic,strong)UITableView* tableViewMessage;
@property(nonatomic,strong)NSMutableArray* arrayMessages;

@end

@implementation HLMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setNavigationTitle:@"消息" Color:[UIColor whiteColor]];
    
    self.arrayMessages = [NSMutableArray new];
    
    [self.view addSubview:self.tableViewMessage];
    [self.tableViewMessage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    [self.tableViewMessage.mj_header beginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrayMessages.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HLMessageCell* cell = [tableView dequeueReusableCellWithIdentifier:@"MessageCell"];
    if (!cell) {
        cell = [[HLMessageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MessageCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.messageObject = self.arrayMessages[indexPath.row];
    return cell;
}

- (void)onRequestMessage:(BOOL)refresh {
    if (refresh) {
        pageNumber = 1;
        [self.tableViewMessage.mj_footer setState:MJRefreshStateIdle];
    } else {
        pageNumber++;
    }
    [HLWebManager getMessageList:pageNumber successBlock:^(id response) {
        if (refresh) {
            [self.tableViewMessage.mj_header endRefreshing];
        } else {
            [self.tableViewMessage.mj_footer endRefreshing];
        }
        if ([response[@"code"]integerValue]==1) {
            if (refresh) {
                [self.arrayMessages removeAllObjects];
            }
            NSArray* array = [HLMessageObject mj_objectArrayWithKeyValuesArray:response[@"list"]];
            if (array.count > 0) {
                [self.arrayMessages addObjectsFromArray:[HLMessageObject mj_objectArrayWithKeyValuesArray:response[@"list"]]];
                [self.tableViewMessage reloadData];
            } else {
                if (!refresh) {
                    [self.tableViewMessage.mj_footer setState:MJRefreshStateNoMoreData];
                }
            }
        }
    } failureBlock:^(NSError *error) {
        if (refresh) {
            [self.tableViewMessage.mj_header endRefreshing];
        } else {
            [self.tableViewMessage.mj_footer endRefreshing];
        }
    }];
}

- (UITableView*)tableViewMessage {
    if (!_tableViewMessage) {
        _tableViewMessage = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableViewMessage.dataSource = self;
        _tableViewMessage.delegate = self;
        _tableViewMessage.hideExtraCell = YES;
        _tableViewMessage.rowHeight = UITableViewAutomaticDimension;
        _tableViewMessage.estimatedRowHeight = 100;
        WeakSelf;
        _tableViewMessage.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakSelf onRequestMessage:YES];
        }];
        _tableViewMessage.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [weakSelf onRequestMessage:NO];
        }];
        _tableViewMessage.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableViewMessage;
}

@end
